program mkhisq ! -- makes histograms from list-mode data file.
  !
  ! DESCRIPTION:
  !
  ! This is a speed-enhanced version of mkhis. Cumbersom, painful
  ! checks about validity of the list-mode data is reduced as much as
  ! possible. It only checks the pair-flags and rejects the event on
  ! the same plane.
  !
  !
  ! LICENSE AGREEMENT:
  !
  ! You agree that we don't agree to anything.
  !
  !
  ! DISCLAIMER OF WARRANTIES:
  !
  ! This software is provided to you "AS IS," and the authors and
  ! contributors disclaim any warranty or liability obligations to you
  ! of any kind, implied or express, including but not limited to the
  ! warranties of merchantability, fitness for a particular purpose,
  ! and non-infringement.
  ! 
  use kuty
  use mkhis_common
  implicit none
  interface
     subroutine initdecvq(a,b,c,d)
       integer*8,intent(in),value::a,b,c,d
     end subroutine initdecvq
  end interface
  interface
     subroutine decvq(pe,ne,pb,nb,pneo,pnbo)
       integer*8,intent(in),value::pe,ne,pb,nb,pneo,pnbo
     end subroutine decvq
  end interface
  integer*8,parameter::nlmdbufmax=737280
  integer nlmdbuf,m
  integer*4,allocatable,target::lmdbuf(:,:)
  integer*8 nlmdbuf_wrote, nbuf_read,nread
  integer pbuf
  integer*2,allocatable,target::hists(:,:,:)
  integer*4,pointer::at1,at2,pd

  call parse_cmdline
  if(ndh/=4) call claimer("*** Number of blocks must be 4, use mkhis otherwise.")

  allocate(hists(0:nxy-1,0:nxy-1,0:1),stat=istat)
  if(istat/=0) call claimer(MEMFAIL)
  hists=0

  call mkhis_init
  pbuf=0
  nxy=nx*ny
  nlmdbuf=0
  call initdecvq(na,nt,ndh,mgap)

  ifile=0
  file: do
     select case(get_next_file(ifile))
     case(-1)
        exit
     case(0)
        cycle
     end select

     ne=fs/4
     n=min(ne,nemax)     
     if(nbuf<n) then
        if(nbuf/=0) deallocate(buf)
        allocate(buf(n),stat=istat)
        if(istat/=0) call claimer(MEMFAIL)
        nbuf=n
     end if
     
     m=min(fs/8,nlmdbufmax)
     if(m>nlmdbuf) then
        if(nlmdbuf/=0) deallocate(lmdbuf)
        allocate(lmdbuf(4,m),stat=istat)
        if(istat/=0) call claimer(MEMFAIL)
        nlmdbuf=m
     end if

     nread=0
     read: do        
        read(1,iostat=istat) buf(1:n)
        if(istat/=0) call claimer(READFAIL)
        nread=nread+n
        nb=nb+n
        pbuf=loc(buf)

        dec: do

           call decvq(pbuf,n,loc(lmdbuf),nlmdbufmax,&
                loc(nbuf_read),loc(nlmdbuf_wrote))
           npair=npair+nlmdbuf_wrote

           sort: do ie=1,nlmdbuf_wrote
              at1=>lmdbuf(1,ie); at2=>lmdbuf(2,ie)
              pd=>lmdbuf(3,ie)
              if(pd==0) ndelay=ndelay+1
              hists(at1,at2,pd)=hists(at1,at2,pd)+1
           end do sort

           if(nbuf_read==n) exit
           pbuf=pbuf+4*nbuf_read
           n=n-nbuf_read

        end do dec

        if(nread==ne) exit          
        n=min(ne-nread,nemax)

     end do read

     close(1)

  end do file

  call print_stats(nf,nb*4,npair,ndelay)

  if(nf>0) call output(nx*ny*nx*ny,&
       loc(hists(0,0,1)),loc(hists(0,0,0)))

end program mkhisq
